package upmc.fr.android.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import android.util.Log;

/**
 * 
 * @author Chamelle
 *
 */
public class Dictionary implements Serializable
{
	private static final long serialVersionUID = 1L;
	private List<String> categories = new LinkedList<String>();
	private HashMap<RSSFlow, List<String>> dictionary = new HashMap<RSSFlow, List<String>>();
	
	public List<String> getCategories()
	{
		return categories;
	}
	
	public void addCategory(String category)
	{
		if (!exists(category))
			this.categories.add(category);
	}
	
	public boolean exists(String category)
	{
		return categories.contains(category);		
	}
	
	public void removeCategory(String category)
	{
		this.categories.remove(category);
		for(RSSFlow feed : dictionary.keySet())
		{
			if(dictionary.get(feed).contains(category))
				dictionary.get(feed).remove(category);
		}
	}
	
	public boolean exists(String category, RSSFlow flow)
	{
		Log.w("--------- DEBUG ----------------", "Dictionary : containsKey : "+dictionary.containsKey(flow));
		Log.w("--------- DEBUG ----------------", "Dictionary : containsCategory : "+dictionary.get(flow).contains(category));
		return dictionary.containsKey(flow) ? dictionary.get(flow).contains(category) : false;		
	}
	
	public boolean exists(String category, String urlFeed)
	{
		for(RSSFlow flow : dictionary.keySet())
		{
			if(flow.getUrl().equals(urlFeed))
			{
				if(dictionary.get(flow).contains(category))
					return true;
			}
		}
		return false;
	}
	
	public void addFeed(String categorie, RSSFlow feed)
	{
		if(!dictionary.containsKey(feed))
			dictionary.put(feed, new LinkedList<String>());		
		dictionary.get(feed).add(categorie);		
		Log.w("--------- DEBUG ----------------", "Dictionary addFeed : "+dictionary.get(feed));

	}
	
	public void removeFeed(String categorie, RSSFlow feed)
	{
		if(dictionary.containsKey(feed)) 
		{
			List<String> categories = dictionary.get(feed);
			if(categories.contains(categorie))
			{
				categories.remove(categorie);
				if(categories.size() == 0)
					dictionary.remove(feed);
			}			
		}		
	}
	
	public void removeFeed(String category, String url) 
	{
	 
	}
	
	public Set<RSSFlow> getFlows()
	{
		return dictionary.keySet();
	}
	
	public List<String> getCategories(RSSFlow feed)
	{
		return dictionary.get(feed);
	}

	public void move(String category_source, String category_target, RSSFlow flow)
	{
		List<String> list = dictionary.get(flow);
		list.remove(category_source);
		if(!list.contains(category_target))
			list.add(category_target);		
	}
	
	public RSSFlow getFeed(String url)
	{
		for(RSSFlow feed : dictionary.keySet())
		{
			if(feed.getUrl().equals(url))
				return feed;
		}
		return null;
	}
}
